home *** CD-ROM | disk | FTP | other *** search
/ Sprite 1984 - 1993 / Sprite 1984 - 1993.iso / src / cmds / gdb.new / gdb-4.0 / gdb / core.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-08-24  |  11.1 KB  |  453 lines

  1. /* Work with core dump and executable files, for GDB.
  2.    Copyright (C) 1986, 1987, 1989 Free Software Foundation, Inc.
  3.  
  4. This file is part of GDB.
  5.  
  6. This program is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 2 of the License, or
  9. (at your option) any later version.
  10.  
  11. This program is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14. GNU General Public License for more details.
  15.  
  16. You should have received a copy of the GNU General Public License
  17. along with this program; if not, write to the Free Software
  18. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
  19.  
  20. #include <stdio.h>
  21. #include <errno.h>
  22. #include <signal.h>
  23. #include "defs.h"
  24. #include "param.h"
  25. #include "frame.h"  /* required by inferior.h */
  26. #include "inferior.h"
  27. #include "symtab.h"
  28. #include "command.h"
  29. #include "bfd.h"
  30. #include "target.h"
  31. #include "gdbcore.h"
  32.  
  33. extern int xfer_memory ();
  34. extern void child_attach (), child_create_inferior ();
  35.  
  36. extern int sys_nerr;
  37. extern char *sys_errlist[];
  38. extern char *sys_siglist[];
  39.  
  40. extern char registers[];
  41.  
  42. /* Hook for `exec_file_command' command to call.  */
  43.  
  44. void (*exec_file_display_hook) () = NULL;
  45.  
  46. /* Binary file diddling handle for the core file.  */
  47.  
  48. bfd *core_bfd = NULL;
  49.  
  50. /* Forward decl */
  51. extern struct target_ops core_ops;
  52.  
  53.  
  54. /* Discard all vestiges of any previous core file
  55.    and mark data and stack spaces as empty.  */
  56.  
  57. /* ARGSUSED */
  58. void
  59. core_close (quitting)
  60.      int quitting;
  61. {
  62.   if (core_bfd) {
  63.     free (bfd_get_filename (core_bfd));
  64.     bfd_close (core_bfd);
  65.     core_bfd = NULL;
  66. #ifdef CLEAR_SOLIB
  67.     CLEAR_SOLIB ();
  68. #endif
  69.     if (core_ops.sections) {
  70.       free (core_ops.sections);
  71.       core_ops.sections = NULL;
  72.       core_ops.sections_end = NULL;
  73.     }
  74.   }
  75. }
  76.  
  77. #ifdef SOLIB_ADD
  78. /* Stub function for catch_errors around shared library hacking. */
  79.  
  80. int 
  81. solib_add_stub (from_tty)
  82.      int from_tty;
  83. {
  84.     SOLIB_ADD (NULL, from_tty, &core_ops);
  85.     return 0;
  86. }
  87. #endif /* SOLIB_ADD */
  88.  
  89. /* This routine opens and sets up the core file bfd */
  90.  
  91. void
  92. core_open (filename, from_tty)
  93.      char *filename;
  94.      int from_tty;
  95. {
  96.   const char *p;
  97.   int siggy;
  98.   struct cleanup *old_chain;
  99.   char *temp;
  100.   bfd *temp_bfd;
  101.   int ontop;
  102.  
  103.   target_preopen (from_tty);
  104.   if (!filename)
  105.     {
  106.       error (core_bfd? 
  107.        "No core file specified.  (Use `detach' to stop debugging a core file.)"
  108.      : "No core file specified.");
  109.     }
  110.  
  111.   filename = tilde_expand (filename);
  112.   if (filename[0] != '/') {
  113.     temp = concat (current_directory, "/", filename);
  114.     free (filename);
  115.     filename = temp;
  116.   }
  117.  
  118.   old_chain = make_cleanup (free, filename);
  119.   temp_bfd = bfd_openr (filename, NULL);
  120.   if (temp_bfd == NULL)
  121.     {
  122.       perror_with_name (filename);
  123.     }
  124.  
  125.   if (!bfd_check_format (temp_bfd, bfd_core))
  126.     {
  127.       bfd_close (temp_bfd);
  128.       error ("\"%s\" does not appear to be a core dump", filename);
  129.     }
  130.  
  131.   /* Looks semi-reasonable.  Toss the old core file and work on the new.  */
  132.  
  133.   discard_cleanups (old_chain);        /* Don't free filename any more */
  134.   unpush_target (&core_ops);
  135.   core_bfd = temp_bfd;
  136.   old_chain = make_cleanup (core_close, core_bfd);
  137.  
  138.   validate_files ();
  139.  
  140.   /* Find the data section */
  141.   if (build_section_table (core_bfd, &core_ops.sections,
  142.                &core_ops.sections_end))
  143.     error ("Can't find sections in `%s': %s", bfd_get_filename(core_bfd),
  144.        bfd_errmsg (bfd_error));
  145.  
  146.   ontop = !push_target (&core_ops);
  147.   discard_cleanups (old_chain);
  148.  
  149.   p = bfd_core_file_failing_command (core_bfd);
  150.   if (p)
  151.     printf ("Core file invoked as `%s'.\n", p);
  152.  
  153.   siggy = bfd_core_file_failing_signal (core_bfd);
  154.   if (siggy > 0)
  155.     printf ("Program terminated with signal %d, %s.\n", siggy,
  156.         siggy < NSIG ? sys_siglist[siggy] : "(undocumented)");
  157.  
  158.   if (ontop) {
  159.     /* Fetch all registers from core file */
  160.     target_fetch_registers (-1);
  161.  
  162.     /* Add symbols and section mappings for any shared libraries */
  163. #ifdef SOLIB_ADD
  164.     (void) catch_errors (solib_add_stub, from_tty, (char *)0);
  165. #endif
  166.     /* Now, set up the frame cache, and print the top of stack */
  167.     set_current_frame ( create_new_frame (read_register (FP_REGNUM),
  168.                       read_pc ()));
  169.     select_frame (get_current_frame (), 0);
  170.     print_sel_frame (0);    /* Print the top frame and source line */
  171.   } else {
  172.     printf (
  173. "Warning: you won't be able to access this core file until you terminate\n\
  174. your %s; do ``info files''\n", current_target->to_longname);
  175.   }
  176. }
  177.  
  178. void
  179. core_detach (args, from_tty)
  180.      char *args;
  181.      int from_tty;
  182. {
  183.   if (args)
  184.     error ("Too many arguments");
  185.   unpush_target (&core_ops);
  186.   if (from_tty)
  187.     printf ("No core file now.\n");
  188. }
  189.  
  190. /* Backward compatability with old way of specifying core files.  */
  191.  
  192. void
  193. core_file_command (filename, from_tty)
  194.      char *filename;
  195.      int from_tty;
  196. {
  197.   dont_repeat ();            /* Either way, seems bogus. */
  198.   if (!filename)
  199.     core_detach (filename, from_tty);
  200.   else
  201.     core_open (filename, from_tty);
  202. }
  203.  
  204.  
  205. /* Call this to specify the hook for exec_file_command to call back.
  206.    This is called from the x-window display code.  */
  207.  
  208. void
  209. specify_exec_file_hook (hook)
  210.      void (*hook) ();
  211. {
  212.   exec_file_display_hook = hook;
  213. }
  214.  
  215. /* The exec file must be closed before running an inferior.
  216.    If it is needed again after the inferior dies, it must
  217.    be reopened.  */
  218.  
  219. void
  220. close_exec_file ()
  221. {
  222. #ifdef FIXME
  223.   if (exec_bfd)
  224.     bfd_tempclose (exec_bfd);
  225. #endif
  226. }
  227.  
  228. void
  229. reopen_exec_file ()
  230. {
  231. #ifdef FIXME
  232.   if (exec_bfd)
  233.     bfd_reopen (exec_bfd);
  234. #endif
  235. }
  236.  
  237. /* If we have both a core file and an exec file,
  238.    print a warning if they don't go together.  */
  239.  
  240. void
  241. validate_files ()
  242. {
  243.   if (exec_bfd && core_bfd)
  244.     {
  245.       if (core_file_matches_executable_p (core_bfd, exec_bfd))
  246.     printf ("Warning: core file does not match specified executable file.\n");
  247.       else if (bfd_get_mtime(exec_bfd) > bfd_get_mtime(core_bfd))
  248.     printf ("Warning: exec file is newer than core file.\n");
  249.     }
  250. }
  251.  
  252. /* Return the name of the executable file as a string.
  253.    ERR nonzero means get error if there is none specified;
  254.    otherwise return 0 in that case.  */
  255.  
  256. char *
  257. get_exec_file (err)
  258.      int err;
  259. {
  260.   if (exec_bfd) return bfd_get_filename(exec_bfd);
  261.   if (!err)     return NULL;
  262.  
  263.   error ("No executable file specified.\n\
  264. Use the \"file\" or \"exec-file\" command.");
  265.   return NULL;
  266. }
  267.  
  268. static void
  269. core_files_info (t)
  270.   struct target_ops *t;
  271. {
  272.   struct section_table *p;
  273.  
  274.   printf ("\tCore file `%s'.\n", bfd_get_filename(core_bfd));
  275.  
  276.   for (p = t->sections; p < t->sections_end; p++)
  277.     if (p->bfd == core_bfd)
  278.       printf("\tcore file  from 0x%08x to 0x%08x is %s\n",
  279.       p->addr, p->endaddr,
  280.       bfd_section_name (p->bfd, p->sec_ptr));
  281.     else {
  282.       printf("\tshared lib from 0x%08x to 0x%08x is %s in %s\n",
  283.       p->addr, p->endaddr,
  284.       bfd_section_name (p->bfd, p->sec_ptr),
  285.       bfd_get_filename (p->bfd));
  286.     }
  287. }
  288.  
  289. void
  290. memory_error (status, memaddr)
  291.      int status;
  292.      CORE_ADDR memaddr;
  293. {
  294.  
  295.   if (status == EIO)
  296.     {
  297.       /* Actually, address between memaddr and memaddr + len
  298.      was out of bounds. */
  299.       error ("Cannot access memory at address 0x%x.", memaddr);
  300.     }
  301.   else
  302.     {
  303.       if (status >= sys_nerr || status < 0)
  304.     error ("Error accessing memory address 0x%x: unknown error (%d).",
  305.            memaddr, status);
  306.       else
  307.     error ("Error accessing memory address 0x%x: %s.",
  308.            memaddr, sys_errlist[status]);
  309.     }
  310. }
  311.  
  312. /* Same as target_read_memory, but report an error if can't read.  */
  313. void
  314. read_memory (memaddr, myaddr, len)
  315.      CORE_ADDR memaddr;
  316.      char *myaddr;
  317.      int len;
  318. {
  319.   int status;
  320.   status = target_read_memory (memaddr, myaddr, len);
  321.   if (status != 0)
  322.     memory_error (status, memaddr);
  323. }
  324.  
  325. /* Same as target_write_memory, but report an error if can't write.  */
  326. void
  327. write_memory (memaddr, myaddr, len)
  328.      CORE_ADDR memaddr;
  329.      char *myaddr;
  330.      int len;
  331. {
  332.   int status;
  333.  
  334.   status = target_write_memory (memaddr, myaddr, len);
  335.   if (status != 0)
  336.     memory_error (status, memaddr);
  337. }
  338.  
  339. /* Read an integer from debugged memory, given address and number of bytes.  */
  340.  
  341. long
  342. read_memory_integer (memaddr, len)
  343.      CORE_ADDR memaddr;
  344.      int len;
  345. {
  346.   char cbuf;
  347.   short sbuf;
  348.   int ibuf;
  349.   long lbuf;
  350.  
  351.   if (len == sizeof (char))
  352.     {
  353.       read_memory (memaddr, &cbuf, len);
  354.       return cbuf;
  355.     }
  356.   if (len == sizeof (short))
  357.     {
  358.       read_memory (memaddr, (char *)&sbuf, len);
  359.       SWAP_TARGET_AND_HOST (&sbuf, sizeof (short));
  360.       return sbuf;
  361.     }
  362.   if (len == sizeof (int))
  363.     {
  364.       read_memory (memaddr, (char *)&ibuf, len);
  365.       SWAP_TARGET_AND_HOST (&ibuf, sizeof (int));
  366.       return ibuf;
  367.     }
  368.   if (len == sizeof (lbuf))
  369.     {
  370.       read_memory (memaddr, (char *)&lbuf, len);
  371.       SWAP_TARGET_AND_HOST (&lbuf, sizeof (lbuf));
  372.       return lbuf;
  373.     }
  374.   error ("Cannot handle integers of %d bytes.", len);
  375.   return -1;    /* for lint */
  376. }
  377.  
  378. /* Get the registers out of a core file.  This is the machine-
  379.    independent part.  Fetch_core_registers is the machine-dependent
  380.    part, typically implemented in the xm-file for each architecture.  */
  381.  
  382. /* We just get all the registers, so we don't use regno.  */
  383. /* ARGSUSED */
  384. static void
  385. get_core_registers (regno)
  386.      int regno;
  387. {
  388.   sec_ptr reg_sec;
  389.   unsigned size;
  390.   char *the_regs;
  391.  
  392.   reg_sec = bfd_get_section_by_name (core_bfd, ".reg");
  393.   size = bfd_section_size (core_bfd, reg_sec);
  394.   the_regs = alloca (size);
  395.   if (bfd_get_section_contents (core_bfd, reg_sec, the_regs,
  396.                 (unsigned)0, size))
  397.     {
  398.       fetch_core_registers (the_regs, size, 0);
  399.     }
  400.   else
  401.     {
  402.       fprintf (stderr, "Couldn't fetch registers from core file: %s\n",
  403.            bfd_errmsg (bfd_error));
  404.     }
  405.  
  406.   /* Now do it again for the float registers, if they exist.  */
  407.   reg_sec = bfd_get_section_by_name (core_bfd, ".reg2");
  408.   if (reg_sec) {
  409.     size = bfd_section_size (core_bfd, reg_sec);
  410.     the_regs = alloca (size);
  411.     if (bfd_get_section_contents (core_bfd, reg_sec, the_regs,
  412.                   (unsigned)0, size))
  413.       {
  414.     fetch_core_registers (the_regs, size, 2);
  415.       }
  416.     else
  417.       {
  418.     fprintf (stderr, "Couldn't fetch register set 2 from core file: %s\n",
  419.          bfd_errmsg (bfd_error));
  420.       }
  421.   }
  422.   registers_fetched();
  423. }
  424.  
  425. struct target_ops core_ops = {
  426.     "core", "Local core dump file",
  427.     "Use a core file as a target.  Specify the filename of the core file.",
  428.     core_open, core_close,
  429.     child_attach, core_detach, 0, 0, /* resume, wait */
  430.     get_core_registers, 
  431.     0, 0, 0, 0, /* store_regs, prepare_to_store, conv_to, conv_from */
  432.     xfer_memory, core_files_info,
  433.     0, 0, /* core_insert_breakpoint, core_remove_breakpoint, */
  434.     0, 0, 0, 0, 0, /* terminal stuff */
  435.     0, 0, 0, 0, /* kill, load, call fn, lookup sym */
  436.     child_create_inferior, 0, /* mourn_inferior */
  437.     core_stratum, 0, /* next */
  438.     0, 1, 1, 1, 0,    /* all mem, mem, stack, regs, exec */
  439.     0, 0,            /* section pointers */
  440.     OPS_MAGIC,        /* Always the last thing */
  441. };
  442.  
  443. void
  444. _initialize_core()
  445. {
  446.  
  447.   add_com ("core-file", class_files, core_file_command,
  448.        "Use FILE as core dump for examining memory and registers.\n\
  449. No arg means have no core file.  This command has been superseded by the\n\
  450. `target core' and `detach' commands.");
  451.   add_target (&core_ops);
  452. }
  453.